package net.cjw.initsb.controller.background;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import net.cjw.initsb.common.ServiceResult;
import net.cjw.initsb.common.ServiceResult.ServiceResultMsg;
import net.cjw.initsb.entity.Permission;
import net.cjw.initsb.service.PermissionServiceI;

@Api(description = "权限")
@RestController
@RequestMapping("permission")
public class PermissionController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private PermissionServiceI permissionServiceI;

	@RequiresRoles("admin")
	@PostMapping("v{version}/create_menu_dir")
	public ServiceResult<String> create_menu_dir(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "节点名称", defaultValue = "管理员管理") @RequestParam String node_name) {

		int state = 0;
		if (Strings.isNullOrEmpty(node_name)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			Permission p = new Permission();
			p.setNodeName(node_name);
			p.setState(state);
			return permissionServiceI.create(p);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}
	@RequiresRoles("admin")
	@PostMapping("v{version}/create_menu_permission")
	public ServiceResult<String> create_menu_permission(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "权限值: 如 post:select:public 拥有所有帖子的增删查改权限,user:select:private 拥有自己发布的帖子的增删改查权限", defaultValue = "role:page") @RequestParam String permission_value,
			@ApiParam(value = "父节点名称,创建根节点不用传", defaultValue = "管理员管理") @RequestParam String pname,
			@ApiParam(value = "节点名称", defaultValue = "角色管理") @RequestParam String node_name,
			@ApiParam(value = "菜单页面地址", defaultValue = "net.cjw.initsb.html") @RequestParam String url) {
		int state = 1;
		if (Strings.isNullOrEmpty(node_name)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			Permission p = new Permission();
			p.setPermissionValue(permission_value);
			p.setNodeName(node_name);
			p.setPid(pname);
			p.setState(state);
			p.setUrl(url);
			return permissionServiceI.create(p);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}
	@RequiresRoles("admin")
	@PostMapping("v{version}/create_handle_permission")
	public ServiceResult<String> create(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "权限值: 如 post:select:public 拥有所有帖子的增删查改权限,user:select:private 拥有自己发布的帖子的增删改查权限", defaultValue = "role:select:public") @RequestParam String permission_value,
			@ApiParam(value = "父节点名称,创建根节点不用传", defaultValue = "角色管理") @RequestParam String pname,
			@ApiParam(value = "节点名称", defaultValue = "查看角色") @RequestParam String node_name) {

		int state = 2;
		if (Strings.isNullOrEmpty(node_name)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			Permission p = new Permission();
			p.setPermissionValue(permission_value);
			p.setNodeName(node_name);
			p.setPid(pname);
			p.setState(state);
			return permissionServiceI.create(p);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}
	@RequiresRoles("admin")
	@PostMapping("v{version}/update")
	public ServiceResult<String> update(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "权限id", defaultValue = "") @RequestParam String id,
			@ApiParam(value = "新权限值", defaultValue = "我是新权限值") @RequestParam(required = false) String permission_value,
			@ApiParam(value = "新url", defaultValue = "我是新url") @RequestParam(required = false) String url,
			@ApiParam(value = "新节点名称", defaultValue = "我是新节点名称") @RequestParam(required = false) String node_name

	) {

		if (Strings.isNullOrEmpty(id)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			Permission p = new Permission();
			p.setId(id);
			p.setPermissionValue(permission_value);
			p.setUrl(url);
			p.setNodeName(node_name);
			return permissionServiceI.update(p);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

	@PostMapping("v{version}/select_all")
	public ServiceResult<Object> selectAll(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "页数", defaultValue = "1") @RequestParam Integer page_num,
			@ApiParam(value = "一页显示item数量", defaultValue = "10") @RequestParam Integer max_result) {
		if (page_num == null || max_result == null) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			return permissionServiceI.selectAll(page_num, max_result);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

	@GetMapping("v{version}/select_by_tree_order")
	public ServiceResult<List<Map<String, Object>>> select_by_tree_order(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version) {
		if (version == 1) {
			return permissionServiceI.selectByTreeOrder();
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

	@GetMapping("v{version}/select_menu_by_list_order")
	public ServiceResult<List<Map<String, Object>>> select_menu_list(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version) {
		if (version == 1) {
			return permissionServiceI.selectMenuByListOrder();
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}
	@GetMapping("v{version}/select_ztree_data")
	public ServiceResult<List<Map<String, Object>>> select_ztree_data(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version) {
		if (version == 1) {
			return permissionServiceI.selectZtreeData();
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}
	@RequiresRoles("admin")
	@PostMapping("v{version}/delete")
	public ServiceResult<String> delete(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "权限id", defaultValue = "1") @RequestParam String id) {
		if (Strings.isNullOrEmpty(id)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			Permission p = new Permission();
			p.setId(id);
			return permissionServiceI.delete(p);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

}
